﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Public Class ActivityDesignerAccessibleObject
        Inherits AccessibleObject
        ' Methods
        Public Sub New(ByVal activityDesigner As ActivityDesigner)
            If (activityDesigner Is Nothing) Then
                Throw New ArgumentNullException("activityDesigner")
            End If
            If (activityDesigner.Activity Is Nothing) Then
                Throw New ArgumentException(DR.GetString("DesignerNotInitialized", New Object(0  - 1) {}), "activityDesigner")
            End If
            Me.activityDesigner = activityDesigner
        End Sub

        Public Overrides Sub DoDefaultAction()
            Dim service As ISelectionService = [TryCast](Me.GetService(GetType(ISelectionService)),ISelectionService)
            If (Not service Is Nothing) Then
                service.SetSelectedComponents(New Object() { Me.activityDesigner.Activity }, SelectionTypes.Replace)
            Else
                MyBase.DoDefaultAction
            End If
        End Sub

        Protected Function GetService(ByVal serviceType As Type) As Object
            If (serviceType Is Nothing) Then
                Throw New ArgumentNullException("serviceType")
            End If
            If ((Not Me.ActivityDesigner.Activity Is Nothing) AndAlso (Not Me.ActivityDesigner.Activity.Site Is Nothing)) Then
                Return Me.ActivityDesigner.Activity.Site.GetService(serviceType)
            End If
            Return Nothing
        End Function

        Public Overrides Function Navigate(ByVal navdir As AccessibleNavigation) As AccessibleObject
            If (navdir = AccessibleNavigation.FirstChild) Then
                Return Me.GetChild(0)
            End If
            If (navdir = AccessibleNavigation.LastChild) Then
                Return Me.GetChild((Me.GetChildCount - 1))
            End If
            Dim parentDesigner As CompositeActivityDesigner = Me.activityDesigner.ParentDesigner
            If (Not parentDesigner Is Nothing) Then
                Dim down As DesignerNavigationDirection = DesignerNavigationDirection.Down
                If (navdir = AccessibleNavigation.Left) Then
                    down = DesignerNavigationDirection.Left
                ElseIf (navdir = AccessibleNavigation.Right) Then
                    down = DesignerNavigationDirection.Right
                ElseIf ((navdir = AccessibleNavigation.Up) OrElse (navdir = AccessibleNavigation.Previous)) Then
                    down = DesignerNavigationDirection.Up
                ElseIf ((navdir = AccessibleNavigation.Down) OrElse (navdir = AccessibleNavigation.Next)) Then
                    down = DesignerNavigationDirection.Down
                End If
                Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner([TryCast](parentDesigner.GetNextSelectableObject(Me.activityDesigner.Activity, down),Activity))
                If (Not designer Is Nothing) Then
                    Return designer.AccessibilityObject
                End If
            End If
            Return MyBase.Navigate(navdir)
        End Function

        Public Overrides Sub [Select](ByVal flags As AccessibleSelection)
            Dim service As ISelectionService = [TryCast](Me.GetService(GetType(ISelectionService)),ISelectionService)
            If (Not service Is Nothing) Then
                If (((flags And AccessibleSelection.TakeFocus) > AccessibleSelection.None) OrElse ((flags And AccessibleSelection.TakeSelection) > AccessibleSelection.None)) Then
                    service.SetSelectedComponents(New Object() { Me.activityDesigner.Activity }, SelectionTypes.Replace)
                ElseIf ((flags And AccessibleSelection.AddSelection) > AccessibleSelection.None) Then
                    service.SetSelectedComponents(New Object() { Me.activityDesigner.Activity }, SelectionTypes.Add)
                ElseIf ((flags And AccessibleSelection.RemoveSelection) > AccessibleSelection.None) Then
                    service.SetSelectedComponents(New Object() { Me.activityDesigner.Activity }, SelectionTypes.Remove)
                End If
            End If
        End Sub


        ' Properties
        Protected ReadOnly Property ActivityDesigner As ActivityDesigner
            Get
                Return Me.activityDesigner
            End Get
        End Property

        Public Overrides ReadOnly Property Bounds As Rectangle
            Get
                Return Me.activityDesigner.InternalRectangleToScreen(Me.activityDesigner.Bounds)
            End Get
        End Property

        Public Overrides ReadOnly Property DefaultAction As String
            Get
                Return DR.GetString("AccessibleAction", New Object(0  - 1) {})
            End Get
        End Property

        Public Overrides ReadOnly Property Description As String
            Get
                Return DR.GetString("ActivityDesignerAccessibleDescription", New Object() { Me.activityDesigner.Activity.GetType.Name })
            End Get
        End Property

        Public Overrides ReadOnly Property Help As String
            Get
                Return DR.GetString("ActivityDesignerAccessibleHelp", New Object() { Me.activityDesigner.Activity.GetType.Name })
            End Get
        End Property

        Public Overrides Property Name As String
            Get
                Dim component As Activity = Me.activityDesigner.Activity
                If (component Is Nothing) Then
                    Return MyBase.Name
                End If
                If (Not TypeDescriptor.GetProperties(component).Item("TypeName") Is Nothing) Then
                    Return [TryCast](TypeDescriptor.GetProperties(component).Item("TypeName").GetValue(component),String)
                End If
                If Not String.IsNullOrEmpty(component.QualifiedName) Then
                    Return component.QualifiedName
                End If
                Return component.GetType.FullName
            End Get
            Set(ByVal value As String)
            End Set
        End Property

        Public Overrides ReadOnly Property Parent As AccessibleObject
            Get
                Dim parentDesigner As CompositeActivityDesigner = Me.activityDesigner.ParentDesigner
                If (parentDesigner Is Nothing) Then
                    Return Nothing
                End If
                Return parentDesigner.AccessibilityObject
            End Get
        End Property

        Public Overrides ReadOnly Property Role As AccessibleRole
            Get
                Return AccessibleRole.Diagram
            End Get
        End Property

        Public Overrides ReadOnly Property State As AccessibleStates
            Get
                Dim states As AccessibleStates = If(Me.activityDesigner.IsSelected, AccessibleStates.Selected, AccessibleStates.Selectable)
                states = (states Or AccessibleStates.MultiSelectable)
                states = (states Or If(Me.activityDesigner.IsPrimarySelection, 4, &H100000))
                If Me.activityDesigner.IsLocked Then
                    states = (states Or AccessibleStates.ReadOnly)
                Else
                    states = (states Or AccessibleStates.Moveable)
                End If
                If Not Me.activityDesigner.IsVisible Then
                    states = (states Or AccessibleStates.Invisible)
                End If
                Return states
            End Get
        End Property


        ' Fields
        Private activityDesigner As ActivityDesigner
    End Class
End Namespace

